package cn.sciento.boot.iam.field.handler;

import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.reflect.FieldUtils;
import cn.sciento.boot.iam.field.dto.FieldPermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

public class HiddenFieldHandler extends SecurityTokenObjectRecursiveHandler {
  private static final Logger logger = LoggerFactory.getLogger(HiddenFieldHandler.class);
  
  private static final String PERMISSION_TYPE = "HIDE";
  
  public int getOrder() {
    return 0;
  }
  
  public void afterProcess(List<FieldPermission> fieldPermissionList, Object arg) {
    if (CollectionUtils.isEmpty(fieldPermissionList) || arg == null)
      return; 
    recursive(fieldPermissionList
        .stream()
        .filter(item -> "HIDE".equals(item.getPermissionType()))
        .collect(Collectors.toList()), arg, (obj, fieldName, fieldPermission) -> FieldUtils.writeDeclaredField(obj, fieldName, null, true));
  }
}
